# -*- encoding:utf8 -*-
from attendance.datetimestub import datetime,datetime_orig
#sys.modules['datetime']=DatetimeStub()

from django.test import TestCase
from leave_apply import submit_leave
from check import approve_leave_app

from datetime import datetime
import leave_apply
leave_apply.timezone=datetime
from attendance.leave import annual
annual.timezone=datetime

from attendance.convert import i2t
from attendance.models import StaffInfo
from attendance.models import LeaveApplication,AvaiLeave,LeaveModel,ConsumeRelation,MsgModel
from attendance.tool_func import get_or_none
from attendance.leave.annual import annual_left_this,annual_left,update_annual,annual_accum
from leave_match import match

class ApplyTest(TestCase):
    def setUp(self):
        self.b,created=StaffInfo.objects.get_or_create(employee_id='AE1857',cn_name=u'白X',startjob_date='2014-01-04',on_board_date='2015-09-07')
        self.staff,created=StaffInfo.objects.get_or_create(employee_id='AE1856',cn_name=u'何X',startjob_date='2014-01-04',on_board_date='2015-09-07',
                                                           func_leader=self.b)

    def test_submit(self):
        print('--test submit_leave')
        leave1 = submit_leave(self.staff, 'sick_leave', '2016-02-20 08:30', '2016-02-20 17:30', u'测试病假')
        leave=get_or_none(LeaveApplication,category='sick_leave')
        self.assertEqual(leave.start_time,'2016-02-20 08:30')
        self.assertEqual(leave.end_time,'2016-02-20 17:30')
        approve_leave_app(leave1)
        self.assertEqual(LeaveApplication.objects.all().count(),1)
        
        msg=MsgModel.objects.get(staff=self.b)
        self.assertEqual(msg.content,str(leave.id))
        self.assertEqual(msg.status,'pending')
        
        datetime.set_now(datetime_orig.datetime(2016,7,30))
        leave2=submit_leave(self.staff, 'annual_leave', '2016-02-20 08:30', '2016-02-20 17:30', u'测试年假')
        self.assertEqual(ConsumeRelation.objects.all().count(),2)
        query=ConsumeRelation.objects.all()
        self.assertEqual(query[0].used_leave_end,'2016-02-20 12:30')
        self.assertEqual(query[1].used_leave_start,'2016-02-20 13:30')
        approve_leave_app(leave2)
        self.assertEqual(LeaveApplication.objects.all().count(),2)
        
    def test_not_enough_submit(self):
        datetime.set_now(datetime_orig.datetime(2016,2,15))
        submit_leave(self.staff, 'annual_leave', '2016-02-20 08:30', '2016-02-20 17:30', u'测试年假')
        query=ConsumeRelation.objects.all()
        self.assertEqual(ConsumeRelation.objects.all().count(),2)
        self.assertEqual(query[0].used_leave_end,'2016-02-20 12:30')
        self.assertEqual(query[1].used_leave_end,'2016-02-20 17:23')        
        
        datetime.set_now(datetime_orig.datetime(2016,7,15))
        update_annual(self.staff)
        match(self.staff)
        query=ConsumeRelation.objects.all()
        self.assertEqual(query.count(),2)  
        self.assertEqual(query[1].used_leave_end,'2016-02-20 17:30')
        self.assertEqual(query[1].span(),240)
        self.assertEqual(annual_left(self.staff),1582)
        self.assertEqual(annual_left_this(self.staff),1582)
        self.assertEqual(annual_accum(self.staff),2062)

    
    def test_match(self):
        datetime.set_now(datetime_orig.datetime(2016,7,30))
        submit_leave(self.staff, 'annual_leave', '2016-02-20 08:30', '2016-02-20 17:30', u'测试年假')
        for r in ConsumeRelation.objects.all():
            print('---testmatch')
            print(r.span())
    
    # def test_import_leave(self):  # 暂时采用 正常导入的方式
        # datetime.set_now(datetime_orig.datetime(2016,2,15))
        # submit_leave(self.staff, 'annual_leave_import', '2016-02-20 08:30', '2016-02-20 17:30', u'测试年假')
        # query=ConsumeRelation.objects.all()
        # self.assertEqual(ConsumeRelation.objects.all().count(),2)
        # self.assertEqual(query[0].used_leave_end,'2016-02-20 12:30')
        # self.assertEqual(query[1].used_leave_end,'2016-02-20 17:23')         
    
class CheckTest(TestCase):
    def test_(self):
        pass